home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / DEVEL2.ZIP / COLOR16.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-13  |  3.5 KB  |  140 lines

  1. /* Routine to setup and compute colors */
  2. /* for 16-color mode                */
  3.  
  4. /* Written by Dave Stampe Mar 21 1992 */
  5.  
  6. #include <stdio.h>
  7. #include <dos.h>
  8. #include "rend386.h"
  9.  
  10.             /* 320x200x16 screen definition */
  11.  
  12. struct Screeninfo screeninfo =
  13.          { 0, 0, 319, 199, 160, 100, 16, 8};
  14.  
  15.              /* colors to use on screen */
  16.  
  17. int screen_clear_color = 15;
  18. int wireframe_color    = 12;
  19. int highlight_color    = 14;
  20. int highest_color      = 15;
  21.  
  22.              /* use linear palette instaed of EGA palette map */
  23.  
  24. static char rst_pal[16] = {0,1,2,3,4,5,20,7,56,57,58,59,60,61,62,63};
  25.  
  26. void read_palette(char *p)   /* save all palette map reg's */
  27. {
  28.     int i;
  29.     union REGS regs;
  30.  
  31.     for (i = 0; i < 16; i++)
  32.         {
  33.         regs.x.ax = 0x1007;
  34.         regs.h.bl = i;
  35.         int86(0x10,®s, ®s);
  36.         p[i] = regs.h.bh;
  37.         }
  38. }
  39.  
  40.  
  41. static void
  42. set_rgb(int i, int r, int g, int b)   /* load DAC slot eq. to palette */
  43. {
  44.     union REGS regs;
  45.  
  46.     regs.x.ax = 0x1010;
  47.     regs.x.bx = rst_pal[i];
  48.     regs.h.dh = r;
  49.     regs.h.ch = g;
  50.     regs.h.cl = b;
  51.     int86(0x10,®s,®s);
  52. }
  53.  
  54.  
  55.  
  56. set_colors()               /* setup desired DAC palette */
  57. {
  58.     int i;
  59.  
  60.     read_palette(rst_pal);
  61.  
  62.     set_rgb(15,10,20,30);        /* screen background */
  63.     for (i = 1; i < 16; i++)
  64.     set_rgb(i-1, i*4, i*3+4, i*2+4);     /* brightnesses */
  65.     return 0;
  66. }
  67.  
  68.  
  69.  
  70. reset_colors()
  71. {
  72.     return 0;
  73. }
  74.  
  75.  
  76.  
  77. extern int poly_cosine(void *p);  /* RETURNS 128*COS(LIGHT ANGLE) */
  78.  
  79. /* USER POLYGON LIGHTING ROUTINE: DETERMINES POLY COLOR # */
  80.  
  81. /* The 16-bit color the user specifies for a polygon is broken down as
  82.    follows:
  83.          H R SS CCCC BBBBBBBB
  84.  
  85.    H is the highlight flag (the polygon should be highlighted in
  86.    some way, usually by outlining it in the highlight_color given above).
  87.  
  88.    R is a reserved bit, which should be set to zero
  89.  
  90.    SS is a two-bit field specifying one of four surface types:
  91.  
  92.       00 is a constant-color surface; the 4-bit field CCCC is ignored, and the
  93.      8-bit field BBBBBBBB is used as an absolute color number
  94.  
  95.       01 is a cosine-lit surface; the 4-bit field CCCC specifies one of 16
  96.      basic colors, and the 8-bit brightness field BBBBBBBB is multiplied
  97.      by the cosine of the angle between the light source and the polygon's
  98.      surface normal to provide a 4-bit shading value.
  99.  
  100.       10 is a pseudo-metallic surface; the CCCC field gives the starting hue,
  101.      and the BBBBBBBB value is ignored.  The color will cycle through
  102.      the different shades to give a 'metallic' effect.
  103.  
  104.       11 is a pseudo-transparent surface made up of alternating rows of
  105.      spaced dots; other than that, it behaves like a pseudo-metallic
  106.      surface.
  107.  
  108.    This routine maps the above into an 8-bit color number in the low byte
  109.    of its return value, and passes through the top four bits.
  110.  
  111.    16-COLOR NOTES:
  112.    The above is the standard input, but mapping to 16-colors is
  113.    difficult.  One method is to support only brightnesses and absolute
  114.    colors (modulo 16).
  115.  
  116.  */
  117.  
  118. int user_poly_color(POLY *p, int pcolor)
  119. {
  120.     int hilite = pcolor & 0xF000;       /* highlight flag  (MSB) */
  121.     unsigned int bright = (pcolor>>1) & 0x7F;  /* mask out albedo (7 bits) */
  122.     unsigned int color;
  123.  
  124.     if ((pcolor & 0x0F00)==0) return ((bright&15) | hilite);  /* abs. color */
  125.  
  126.     if ((pcolor & 0x3000) == 0)  /* fixed (unlit) color */
  127.         {
  128.         if(bright>14) bright = 14;
  129.         return (hilite | bright);
  130.         }
  131.  
  132.     color = ((poly_cosine(p)+200) * bright) / 0x0A20;
  133.  
  134.     if (color < 0)  return (hilite);
  135.     if (color > 14) return (14 | hilite);
  136.     return (color | hilite);
  137. }
  138.  
  139.  
  140.